package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.exception.CustomException;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class categoryServiceImpl extends ServiceImpl <CategoryMapper,Category> implements CategoryService {

    @Autowired
    private DishService dishService;
    @Autowired
    private SetmealService setmealService;

    @Override
    public void remove(Long id) {
        //添加查询条件,根据分类id进行查询菜品数据
        LambdaQueryWrapper<Dish> dishLQW = new LambdaQueryWrapper<>();
        //这是查菜品表里面的套餐关系id是否有关联的套餐id,有就代表本菜品是哪个套餐或者菜系下的菜品
        dishLQW.eq(Dish::getCategoryId,id);
        //记录查到的本次要删除的套餐或者菜系下是否有关联的菜品,做记录来做条件判断
        int dishCount = dishService.count(dishLQW);
        if(dishCount>0){
            throw new CustomException("当前分类下关联的菜品,不能删除!");
        }

        //查询当前分类是否关联了套餐，如果已经关联，抛出一个业务异常
        LambdaQueryWrapper<Setmeal> SetmealLQW = new LambdaQueryWrapper<>();
        SetmealLQW.eq(Setmeal::getCategoryId,id);
        int count = setmealService.count(SetmealLQW);
        if(dishCount>0){
            throw new CustomException("当前分类下关联的套餐,不能删除!");
        }
        //正常删除分类
        super.removeById(id);
    }
}
